﻿using System;
using Sinacor.OMM.Util;

namespace Sinacor.OMM.Deployment.Setup.Steps
{
    public class InstallWindowsService
    {
        #region [ Public Methods ]

        public void Execute(string serviceName, string displayName, string fileName, Parameter.Enums.SetupMode setupMode)
        {
            if (setupMode == Parameter.Enums.SetupMode.Install)            
                this.InstallServices(serviceName, displayName, fileName);            
            else            
                this.UnistallServices(serviceName, displayName, fileName);                    
        }

        public bool VerifyInstallService(string serviceName)
        {
            if (WindowsService.GetServiceInfo(serviceName) != null)
                return true;

            return false;
        }

        public string GetServiceName(Parameter.Enums.Components component)
        {
            System.Configuration.AppSettingsReader appReader = new System.Configuration.AppSettingsReader();

            if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Receptor)
                return appReader.GetValue("ReceptorServiceName", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Router)
                return appReader.GetValue("RoteadorServiceName", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Dumper)
                return appReader.GetValue("DumperServiceName", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HFTProcess)
                return appReader.GetValue("HFTServiceName", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.OrderExecutor)
                return appReader.GetValue("OrderExecutorServiceName", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HealthMonitor)
                return appReader.GetValue("HealthMonitorServiceName", typeof(System.String)).ToString();

            return "";
        }

        public string GetServiceDisplayName(Parameter.Enums.Components component)
        {
            System.Configuration.AppSettingsReader appReader = new System.Configuration.AppSettingsReader();

            if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Receptor)
                return appReader.GetValue("ReceptorServiceDisplayName", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Router)
                return appReader.GetValue("RoteadorServiceDisplayName", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Dumper)
                return appReader.GetValue("DumperServiceDisplayName", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HFTProcess)
                return appReader.GetValue("HFTServiceDisplayName", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.OrderExecutor)
                return appReader.GetValue("OrderExecutorServiceDisplayName", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HealthMonitor)
                return appReader.GetValue("HealthMonitorServiceDisplayName", typeof(System.String)).ToString();

            return "";
        }

        public string GetServiceExe(Parameter.Enums.Components component)
        {
            System.Configuration.AppSettingsReader appReader = new System.Configuration.AppSettingsReader();

            if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Receptor)
                return appReader.GetValue("ReceptorServiceExe", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Router)
                return appReader.GetValue("RoteadorServiceExe", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Dumper)
                return appReader.GetValue("DumperServiceExe", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HFTProcess)
                return appReader.GetValue("HFTServiceExe", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.OrderExecutor)
                return appReader.GetValue("OrderExecutorServiceExe", typeof(System.String)).ToString();
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HealthMonitor)
                return appReader.GetValue("HealthMonitorServiceExe", typeof(System.String)).ToString();

            return "";
        }

        #endregion

        #region [ Private Methods ]

        private void InstallServices(string serviceName, string displayName, string fileName)
        {
            WindowsService.ServiceInfo serviceInfo = WindowsService.GetServiceInfo(serviceName);

            string message = string.Empty;

            if (serviceInfo == null)
            {
                if (!WindowsService.InstallService(serviceName, fileName, out message))
                {
                    throw new Exception(string.Format("Não foi possível instalar o serviço {0}!" + Environment.NewLine + message, displayName));
                }               
            }
            else
            {
                throw new Exception(string.Format("Não foi possível instalar o serviço {0}, o serviço já encontra-se instalado!" + Environment.NewLine + message, displayName));
            }
        }

        private void UnistallServices(string serviceName, string displayName, string fileName)
        {
            WindowsService.ServiceInfo serviceInfo = WindowsService.GetServiceInfo(serviceName);

            string message = string.Empty;

            if (serviceInfo != null)
            {
                if (!WindowsService.UninstallService(serviceName, fileName))
                {
                    throw new Exception(string.Format("Não foi possível instalar o serviço {0}!" + Environment.NewLine + message, displayName));
                }   
            }
            else
            {
                throw new Exception(string.Format("Não foi possível desinstalar o serviço {0}, o serviço não está instalado!" + Environment.NewLine + message, displayName));
            }
        }

        #endregion
    }
}